<?php

namespace App\Admin\Controllers\Plate;

use App\Models\Plate\Version;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class VersionController extends Controller
{
    use HasResourceActions;
    public $header = '版本管理';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Version::class, function (Grid $grid) {

            $grid->model()->orderBy('id','desc');
            $grid->id('编号')->sortable();
            $grid->vcode('版本号');
            $grid->column('版本名')->display(function (){
                return $this->version1 . '.' . $this->version2 . '.' . $this->version3;
            });
            $grid->package_size('包大小(M)');
            $grid->content('升级内容');
            $grid->download_url('下载地址')->display(function ($v) {
                return config('filesystems.disks.qiniu.url') . $v;
            });
            $grid->enforce('是否强制更新')->switch([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'danger'],
            ]);
            /*$grid->enforce('是否强制更新');
            $grid->status('状态');*/
            $grid->created_at('创建时间');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Version::class, function (Form $form) {
            $form->text('vcode', '版本号');

            $arr = [];
            for ($i = 1; $i <= 100; $i++) {
                $arr[$i] = $i;
            }

            $form->select('version1','版本名')->options($arr);
            $form->select('version2','')->options($arr);
            $form->select('version3', '')->options($arr);

            //$form->text('package_size', '包大小(M)');
            $form->textarea('content', '升级内容');
            $form->file('download_url', '包上传');
            //$form->largefile('download_url', '包上传');
            $form->radio('enforce', '是否强制更新')->options([
                '1' => '是',
                '0' => '否',
            ])->default('1');
            $form->radio('status', '状态')->options([
                '1' => '正常',
                '0' => '隐藏',
            ])->default('1');

            $form->disableViewCheck();
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->tools(function (Form\Tools $tools){
                $tools->disableView();
            });

            $form->saving(function (Form $form) {
                $size = number_format($form->download_url->getSize() / 1024 / 1024, 2, '.', '');

                $form->model()->package_size = $size;


                //$form->download_url = config('disks.qiniu.url') . $form->download_url;

            });


        });
    }

}
